//
//  LKBackCommissionSonViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/9/18.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKBackCommissionSonViewController.h"
#import "LKPickeCustomView.h"
#import "LKBenefitModel.h"
#import "UIButton+Extension.h"
@interface LKBackCommissionSonViewController ()<GetContentInformation>
@property (weak, nonatomic) IBOutlet UIButton *timeBtn;
@property (weak, nonatomic) IBOutlet UILabel *chooseSubLabel;
@property (weak, nonatomic) IBOutlet UILabel *chooseAgentLabel;
@property (weak, nonatomic) IBOutlet UILabel *chooseShopLabel;

@property(nonatomic,strong)LKBenefitModel *chooseBenefitModel;
@property(nonatomic,strong)LKPickeCustomView *sorcepickerView;
@property(nonatomic,strong)NSMutableArray *dateArray;
@end

@implementation LKBackCommissionSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //选择当前日期的前一天
    NSDate *currentPreDate = [self getPriousorLaterDateFromDate:[NSDate date] withTime:-1 andType:2];
    NSString *currentPreDateStr = [NSString getTimeStrAccoringDate:currentPreDate];
    if (self.timeType == LKBackCommissionTypeDay) {
        NSString *timeStr = [[currentPreDateStr componentsSeparatedByString:@" "] firstObject];
        NSInteger day = [[timeStr componentsSeparatedByString:@"-"][2] integerValue];
        for (int i = 0; i < day; i++) {
            NSString *dateStr = [NSString getTimeStrAccoringDate:[self getPriousorLaterDateFromDate:currentPreDate withTime:-i andType:2]];
            NSString *timeStr = [dateStr substringToIndex:10];
            [self.dateArray addObject:timeStr];
        }
    }else if (self.timeType == LKBackCommissionTypeMonth){
        for (int i = 0; i < 6; i++) {
            NSString *dateStr = [NSString getTimeStrAccoringDate:[self getPriousorLaterDateFromDate:currentPreDate withTime:-i andType:1]];
            NSString *timeStr = [dateStr substringToIndex:7];
            [self.dateArray addObject:timeStr];
        }
    }else{
        for (int i = 0; i < 2; i++) {
            NSString *dateStr = [NSString getTimeStrAccoringDate:[self getPriousorLaterDateFromDate:currentPreDate withTime:-i andType:0]];
            NSString *timeStr = [dateStr substringToIndex:4];
            [self.dateArray addObject:timeStr];
        }
    }
    [self.timeBtn setTitle:[self.dateArray firstObject] forState:UIControlStateNormal];
    [self.timeBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    [self.timeBtn layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:5];
    [self getAllBenefitData];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)timeShowChoose:(id)sender {
    WS(weakSelf)
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    _sorcepickerView = [[LKPickeCustomView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _sorcepickerView.font = [UIFont systemFontOfSize:20];
    _sorcepickerView.delegate = weakSelf;
    _sorcepickerView.contentArray = weakSelf.dateArray;
    _sorcepickerView.selectValue = _sorcepickerView.contentArray[0];
    [_sorcepickerView.pickView reloadAllComponents];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self.sorcepickerView];
}
/**
 取消的代理放啊
 */
-(void)ContentPickerCancleActionAndPickerView:(UIPickerView *)pickeView{
    [self.sorcepickerView removeFromSuperview];
}

- (void)getContentInformation:(NSString *)sex AndPickerView:(UIPickerView *)pickeView {
    [self.sorcepickerView removeFromSuperview];
    [self.timeBtn setTitle:sex forState:UIControlStateNormal];
    [self getAllBenefitData];
}
-(NSMutableArray *)dateArray{
    if (_dateArray == nil) {
        _dateArray = [[NSMutableArray alloc] init];
    }
    return _dateArray;
}
/// 获取我的代理和商户的收益数据
-(void)getAllBenefitData{
    [SVProgressHUD show];
    NSDictionary *param;
    NSString *timeStr = self.timeBtn.currentTitle;
    NSArray *timeArray = [timeStr componentsSeparatedByString:@"-"];
    if (timeArray.count == 1) {
        param = @{@"token":[LKUserInforTool account].token,@"uid":[LKUserInforTool account].uid,@"year":timeArray[0]};
    }else if (timeArray.count == 2){
        NSString *monthStr = timeArray[1];
        if ([monthStr hasPrefix:@"0"]) {
            monthStr = [monthStr substringFromIndex:1];
        }
        param = @{@"token":[LKUserInforTool account].token,@"uid":[LKUserInforTool account].uid,@"year":timeArray[0],@"month":monthStr};
    }else{
        NSString *monthStr = timeArray[1];
        if ([monthStr hasPrefix:@"0"]) {
            monthStr = [monthStr substringFromIndex:1];
        }
        NSString *dayStr = timeArray[2];
        if ([dayStr hasPrefix:@"0"]) {
            dayStr = [dayStr substringFromIndex:1];
        }
        param = @{@"token":[LKUserInforTool account].token,@"uid":[LKUserInforTool account].uid,@"year":timeArray[0],@"month":monthStr,@"day":dayStr};
    }

    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyBenefitUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
         self.chooseBenefitModel = [LKBenefitModel modelWithDictionary:responseObject];
         self.chooseShopLabel.text = self.chooseBenefitModel.getmisson;
         self.chooseAgentLabel.text = self.chooseBenefitModel.merchant_data;
         self.chooseSubLabel.text = self.chooseBenefitModel.sum;
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }

    } failure:^(NSString * _Nullable errorMessage) {
        [SVProgressHUD dismiss];
    }];
}
/// 获取当前日期的前一部分时间
/// @param date 当前日期
/// @param month 月份
- (NSDate*)getPriousorLaterDateFromDate:(NSDate*)date withTime:(int)month andType:(NSInteger)type{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    if (type == 0) {
          [comps setYear:month];
    }else if (type == 1){
          [comps setMonth:month];
    }else{
        [comps setDay:month];
    }
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
